package com.woniu.microuserserver.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.RoleInfoEntity;
import com.woniu.microcommonsserver.entity.RolePermEntity;
import com.woniu.microcommonsserver.entity.UserInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microuserserver.bean.PremsOfRole;
import com.woniu.microuserserver.bean.RoleQuery;
import com.woniu.microuserserver.bean.RoleUserQuery;
import com.woniu.microuserserver.service.RoleService;
import com.woniu.microuserserver.service.UserService;
import com.woniu.microuserserver.utils.MyArrayUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;
    //根据条件查询所有角色信息
    @RequestMapping("/tabledata")
    public ResponseMsg searchRole(@RequestBody RoleQuery roleQuery){
        PageInfo<RoleInfoEntity> pageInfo = roleService.searchByQuery(roleQuery);
        return ResponseMsg.builder().code(200).msg("查询成功").data(pageInfo).build();
    }

    @RequestMapping("/updateRole")
    public ResponseMsg updateRole(@RequestBody RoleInfoEntity roleInfoEntity){
        System.out.println(roleInfoEntity);
        int i = roleService.updateRole(roleInfoEntity);
        if (i!=0){
            return ResponseMsg.builder().code(200).msg("修改成功").build();
        }else {
            return ResponseMsg.builder().code(10000).msg("失败").build();
        }
    }

    //添加角色信息
    @RequestMapping("/addRole")
    public ResponseMsg addRole(@RequestBody RoleInfoEntity roleInfoEntity){
        int i = roleService.addRole(roleInfoEntity);
        if (i!=0){
            return ResponseMsg.builder().code(200).msg("修改成功").build();
        }else {
            return ResponseMsg.builder().code(10000).msg("失败").build();
        }
    }

    //删除角色
    @DeleteMapping("/del/{rid}")
    public ResponseMsg delRole(@PathVariable int rid){
        //判断用户角色中间表中是否绑定了用户
        int i = roleService.checkUidByrid(rid);
        if (i!=0){
            return ResponseMsg.builder().code(10000).msg("请先处理绑定用户").build();
        }else{
            //删除操作
            int i1 = roleService.delRole(rid);
            return ResponseMsg.builder().code(200).msg("删除成功").build();
        }
    }

    //根据id修改角色状态
    @RequestMapping("/updateRoleStatus")
    public ResponseMsg updateRoleStatus(@RequestBody RoleInfoEntity roleInfoEntity){
        int i = roleService.updateRoleStatus(roleInfoEntity);
        if (i!=0){
            return ResponseMsg.builder().code(200).msg("修改成功").build();
        }else{
            return ResponseMsg.builder().code(10000).msg("修改失败").build();
        }
    }

    //添加角色与用户的关系
    @RequestMapping("/addUserToRole")
    public ResponseMsg addUserToRole(int rid,int[] uids){
        System.out.println(rid);
        System.out.println(uids);
        //首先移除这个角色所拥有的用户
        roleService.removeUserFromRole(rid);
        //再添加
        for (int i = 0; i <uids.length ; i++) {
            roleService.addUserToRole(rid, uids[i]);
        }
        return ResponseMsg.builder().code(200).msg("添加成功").build();
    }

    //查找一个角色所没拥有的用户
    @RequestMapping("/findTheRoleNotHasUsers/{rid}")
    public ResponseMsg findTheRoleNotHasUsers(@PathVariable int rid){
        System.out.println(rid);
        //先查询所有的员工信息
        List<UserInfoEntity> listall = userService.searchfour();
        System.out.println("所有的"+listall);
        //再查询这个角色已经拥有的用户id
        List<Integer> uids = roleService.findTheRoleHasUser(rid);
        //得到这个角色所没有的用户

        System.out.println("需要排除的"+uids);

//        for (int i = 0; i <listall.size() ; i++) {
//            for (int j = 0; j < uids.size(); j++) {
//                if (listall.get(i).getId()==uids.get(j).longValue()){
//                    System.out.println("执行移除");
//                    listall.remove(listall.get(i));
//                }else{
//                    System.out.println("不执行移除");
//                }
//            }
//        }
//        System.out.println("最终的"+listall);
        ArrayList list = new ArrayList();
        list.add(uids);
        list.add(listall);
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    //给角色id添加权限
    @RequestMapping("/addPermToRole/{rid}")
    public ResponseMsg addPermToRole(@PathVariable int rid,int[] home,int[] workordermange,int[] busconfig,int[] usercentre){
        //先清空这个角色的权限信息
        roleService.delPermByRid(rid);
        //权限数据相加
        int hp = MyArrayUtils.sumArray(home);
        int wp = MyArrayUtils.sumArray(workordermange);
        int bp = MyArrayUtils.sumArray(busconfig);
        int up = MyArrayUtils.sumArray(usercentre);
        //添加权限数据
        int i = roleService.addPermToRole(rid, hp, wp, bp, up);
        return ResponseMsg.builder().code(200).msg("添加成功").build();
    }

    //返回某个角色的权限数据
    @RequestMapping("/getPermByRoleId/{rid}")
    public ResponseMsg getPermByRoleId(@PathVariable int rid){
        RolePermEntity permEntity = roleService.getPermsByRid(rid);
        PremsOfRole prems = MyArrayUtils.getPremOfRole(permEntity);
        return ResponseMsg.builder().code(200).msg("查询成功").data(prems).build();
    }
}
